$(function () {
    $("#page").load("/html/page.html");
})

function getCount() {
    let aa = {
        username: $("#username").val(),
        sex: $("#sex").val(),
        phone: $("#phone").val(),
        email: $("#email").val(),
        startBirthday: $("#startBirthday").val(),
        endBirthday: $("#endBirthday").val(),
        hobby: getHobby(),
        enable: $('input:radio[name="enable"]:checked').val(),
        curPage: $("#curPage").val(),
        pageSize: $("#pageSize").val()
    };
    let data = myAjax("/back/user/getCount", aa, "get");
    $("#totalPage").html(data.totalCount);
    console.log(data);
    page('', data.totalCount);
}

function findAll() {
    let data = {
        username: $("#username").val(),
        sex: $("#sex").val(),
        phone: $("#phone").val(),
        email: $("#email").val(),
        startBirthday: $("#startBirthday").val(),
        endBirthday: $("#endBirthday").val(),
        hobby: getHobby(),
        enable: $('input:radio[name="enable"]:checked').val(),
        curPage: $("#curPage").val(),
        pageSize: $("#pageSize").val()
    };
    console.log(data);
    $.ajax({
        url: '/back/user/findAll',//访问后台servlet的地址
        data: data,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            $("#totalPage").html(data.totalCount);
            setData(data.list);
        }
    })
}

function getHobby() {
    let hobby = '';
    $('input:checkbox[name=hobby]').each(function () {
        if ($(this).prop('checked')) {
            hobby += $(this).val() + ",";
        }
    });
    return hobby;
}

function setData(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += '<div>\n' +
            '            <div>' + data[i].id + '</div>\n' +
            '            <div>' + data[i].username + '</div>\n' +
            '            <div>' + data[i].sex + '</div>\n' +
            '            <div>' + getString(data[i].phone) + '</div>\n' +
            '            <div>' + getString(data[i].hobby) + '</div>\n' +
            '            <div>' + getString(data[i].address) + '</div>\n' +
            '            <div>' + getTime(data[i].createTime) + '</div>\n' +
            '            <div>' + getTime(data[i].birthday) + '</div>\n' +
            '            <div>' + (data[i].enable == 1 ? '已激活' : '未激活') + '</div>\n' +
            '            <div><div onclick="alertEditWindow(' + data[i].id + ')">编辑</div> ' +
            '<div onclick="del(' + data[i].id + ')">删除</div></div>\n' +
            '        </div>';
    }
    $("#tbody").html(html);
}

// let userId = 0;
function del(id) {
    let result = myAjax("/back/user/del", {id: id}, "post");
    if (result.result == 1) {
        findAll();
    } else {
        alert("删除失败");
    }
}

function alertEditWindow(id) {
    // userId = id;
    sessionStorage.setItem("userId", id);
    $("#editTitle").html("用户编辑窗口");
    $("#editWindow").show();
    $("#editBody").load("/html/back/user/userEdit.html");
}

function closeEditWindow() {
    $("#editWindow").hide();
}

